package com.ruicar.afs.cloud.basic.proceeds.debit.service.impl.thread;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.proceeds.debit.entity.BasicDebitRecord;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitBatchService;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitRecordService;
import com.ruicar.afs.cloud.common.modules.contract.enums.DebitStatusEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-06-26 17:15
 */
@Slf4j
public class ProduceDebitBatchThread implements Callable<Integer> {

    private BasicDebitBatchService basicDebitBatchService;
    private BasicDebitRecordService basicDebitRecordService;
    private Date batchDate;

    private String debitSignBank;
    Map<String,List<String>> bankContractNo;
    private int start;
    private int end;

    public ProduceDebitBatchThread(Map<String,List<String>> bankContractNo,String debitSignBank, int start, int end,BasicDebitBatchService basicDebitBatchService,BasicDebitRecordService basicDebitRecordService, Date batchDate) {
        this.basicDebitRecordService = basicDebitRecordService;
        this.basicDebitBatchService = basicDebitBatchService;
        this.debitSignBank = debitSignBank;
        this.bankContractNo = bankContractNo;
        this.start = start;
        this.batchDate = batchDate;
        this.end = end;
    }

    @Override
    public Integer call() throws Exception {
        /**  查询所有符合查询条件的待扣款数据  **/
        List<BasicDebitRecord> basicDebitRecordList = basicDebitRecordService.list(Wrappers.<BasicDebitRecord>query().lambda()
                .eq(BasicDebitRecord::getDebitStatus, DebitStatusEnum.mayDebit)
                /**  合同编号 by ZC.GUO  **/
                .in(BasicDebitRecord::getContractNo,bankContractNo.get(debitSignBank).subList(start,end))
                .eq(BasicDebitRecord::getDebitSignBank,debitSignBank)
        );
        if (basicDebitRecordList != null && basicDebitRecordList.size() > 0){
            return basicDebitBatchService.generateDebitBatchByList(basicDebitRecordList,batchDate);
        }else{
            return 0;
        }
    }
}
